let app = getApp()
import detailCanvas from '../utils/shareliveCanvas.js';
Component({
    properties: {
      item: {
        type:Object
      },
          list: {
            type: Array,
            observer: function(newVal, oldVal) {
              this.updatelivelist()
            }
          },
          listStyle: {
            type:String
          }
      },
      data:{
        loginShow:false
      },
      
    lifetimes: {
      ready:function(){
        wx.getSystemInfo({
          success: (res)=>{
            this.setData({
              screen_width: res.windowWidth/375,
              screen_height: res.windowHeight
           })
          },
        });
      },
        attached: function () {
 
         }
      },
    methods: {
      cancenlsharefriend:function(){
        this.setData({
          shareDialogPosterVisible:false,
          shareDialogPosterMask:false
        })
      },
    saveShareImg:function(){
      let posters = this.data.posters;
      wx.saveImageToPhotosAlbum({
       filePath: posters,
       success: (res)=>{
         wx.showToast({
           title:'保存成功',
           icon:'success'
         })
         this.setData({
           posters:'',
           shareDialogPosterVisible:false,
           shareDialogPosterMask:false
         })
       },
       fail: (err)=>{
         wx.showToast({
           title: '请授权保存图片',
           icon: 'none',
         });
         this.setData({
           isShare:true
         })
       },
     })
    },
    sharehaibao:function(){
        wx.showLoading({
          title: '绘制中',
          mask: true,
        });
        let that = this;
        let roomid =that.data.roomid;
        wx.request({
          url: app.globalData.siteroot,
          data: {
            do:'Qrcode_getQrCode',
            path:'yb_shopv2/native/all/index?type=l_i&roomId='+roomid+'&pid=0&sid=0'
          },
          success: function(res) {
               let market_price=0;
               let price=0;
               let title =that.data.share_name;
               let share_img =that.data.share_img;
               let codeImg =res.data.qrurl;
               var user=wx.getStorageSync('userinfo')
               let nickName = user.nick_name
               let headUrl = app.globalData.siteDomain+'/addons/yb_shopv2_video/core/head_convert.php?url='+user.user_headimg;
               Promise.all([
                that.getCode(share_img),
                that.getCode(codeImg),
                that.getCode(headUrl)
               ]).then(res=>{
                   let sWidth = that.data.screen_width;
                   let sHeight = that.data.screen_height;
                   let img = res[0].tempFilePath;
                   let code = res[1].tempFilePath;
                   let headimg = res[2].tempFilePath;
                   detailCanvas(that,sWidth,sHeight,headimg,img,code,title,nickName,function(res){
                   wx.hideLoading();
                   that.setData({ 
                     posters:res
                   });
                 });
               })
          }
        })
            this.setData({
              saveText:'保存到相册，分享给好友',
              shareDialogVisible:false,
              shareDialogPosterVisible:true,
              shareDialogPosterMask:true
            })
       },
        looklive:function(e){
            const key = e.currentTarget.dataset.key;
            if(key==107)
            {
                wx.showToast({
                    title: '直播已过期',
                    icon: 'none',
                    duration: 1500
                })
            }
            if(key==103||key==101)
            {
                let roomid = e.currentTarget.dataset.roomid;
                if (roomid){
                    wx.navigateTo({ 
                    url: `plugin-private://wx2b03c6e691cd7370/pages/live-player-plugin?room_id=${roomid}`
                  })
                }
            }
        },
        cancelshare:function(){
            this.setData({
                shareDialogVisible:false,
                shareDialogMask:false
            })
        },
        sharebap:function(e){
            const curIndex=e.currentTarget.dataset.liveid;
            var roomid=this.data.list[curIndex].roomid
            var share_img=this.data.list[curIndex].share_img
            var share_name=this.data.list[curIndex].name
            this.setData({
                share_name:share_name,
                share_img:share_img,
                roomid:roomid,
                shareDialogVisible:true,
                shareDialogMask:true
            })
            this.triggerEvent('sharefriend',this.data.list[curIndex])
        },
        getCode(img){
          return new Promise((resolve,reject)=>{
            wx.downloadFile({
              url: img,
              success: (res) => {
                resolve(res)
              }
            })
          })
        },
        updatelivelist:function(){
            var that=this
             this.setData({
                list:that.data.list
             })
        }
    }
});
